package cn.common.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import cn.common.resource.ResourceManager;

public class WatermarkThread /* implements Runnable */{

	private String url;

	private String path;

	private String localPath;

	private String imageName;

	public WatermarkThread() {
		super();
	}

	public WatermarkThread(String url, String path, String imageName) {
		super();
		this.url = url;
		this.path = path;
		this.imageName = imageName;
		URL s = WatermarkThread.class.getClassLoader().getResource("");
		localPath = s.getPath().substring(0, s.getPath().lastIndexOf("WEB-INF"));
	}

	public void run() {
		createImageScale();
	}

	private void createImageScale() {
		String water = localPath + "water/water_logo.png";
		String srcImage = null;
		try {
			ResourceManager.init();
			url = ResourceManager.getString("remote.url") + url.replace("/amsupload/", "/");
			URL u = new URL(url);
			String urlPath = u.getPath().substring(u.getPath().lastIndexOf("/") + 1);
			String fileName = urlPath.substring(0, urlPath.lastIndexOf("."));
			String fileType = urlPath.substring(urlPath.lastIndexOf(".") + 1);
			BufferedImage source = ImageIO.read(u);
			srcImage = localPath + "houseImag/web/" + path + "" + fileName + "." + fileType;
			File file = new File(srcImage);
			if (!file.exists()) {
				file.mkdirs();
			}
			ImageHandlerUtils ImageHandlerUtils = new ImageHandlerUtils();
			ImageIO.write(source, fileType, file);
			String v800_600_no = localPath + "houseImag/web/" + path + "v800x600_no_" + fileName + "." + fileType;
			ImageHandlerUtils.createThumb(srcImage, v800_600_no, 800, 600);
			// ImageHandlerUtils.createThumnImage(srcImage, v800_600_no, 800,
			// 600);
			String v800_600 = localPath + "houseImag/web/" + path + "v800x600_" + fileName + "." + fileType;
			ImageHandlerUtils.playWatermark(srcImage, v800_600, water, 800, 600);
			// ImageHandlerUtils.playWatermarkImage(v800_600_no, v800_600,
			// water, 800, 600);
			String v480_360 = localPath + "houseImag/web/" + path + "v480x600_" + fileName + "." + fileType;
			ImageHandlerUtils.createThumb(v800_600, v480_360, 480, 360);
			// ImageHandlerUtils.createThumnImage(v800_600, v480_360, 480, 360);
			String v180_135 = localPath + "houseImag/web/" + path + "v180x135_" + fileName + "." + fileType;
			ImageHandlerUtils.createThumb(v800_600, v180_135, 180, 135);
			// ImageHandlerUtils.createThumnImage(v800_600, v180_135, 180, 135);
			String v88_66 = localPath + "houseImag/web/" + path + "v88x66_" + fileName + "." + fileType;
			ImageHandlerUtils.createThumb(v800_600, v88_66, 88, 66);
			// ImageHandlerUtils.createThumnImage(v800_600, v88_66, 88, 66);
		} catch (MalformedURLException e) {
			System.out.println(e.getMessage());
			//e.printStackTrace();
		} catch (IOException e) {
			System.out.println("*********image path =" +srcImage + ";  http get image error: " + e.getMessage());
			//e.printStackTrace();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			//e.printStackTrace();
		}
	}

}
